package com.cat.model.web.action.admin.album;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.AlbumService;
import com.cat.model.common.model.common.AlbumVO;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class AlbumManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private AlbumVO album;
	private Long id;
	private File coverImage;
	private String coverImageFileName; 
	private String coverImageUrl;
		
	@Autowired
	private AlbumService albumService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAlbumSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<AlbumVO> list = (List<AlbumVO>)plmCommDAO.search("manager.searchAlbum", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		albumService.deleteAlbum(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		try {
			if(coverImage != null){
				BufferedImage img = ImageIO.read(new FileInputStream(coverImage));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			}
			albumService.createAlbum(album, coverImage, coverImageFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		album = modelMapper.getAlbum(id);
		if(album != null){
			coverImageUrl = ImageUtil.wrapImageUrl(album.getCoverImage());
		}
		return "edit";
	}
	
	public String edit(){
		try {
			if(coverImage != null){
				BufferedImage img = ImageIO.read(new FileInputStream(coverImage));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			}
			albumService.updateAlbum(album, coverImage, coverImageFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}

	public AlbumVO getAlbum() {
		return album;
	}

	public void setAlbum(AlbumVO album) {
		this.album = album;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public File getCoverImage() {
		return coverImage;
	}

	public void setCoverImage(File coverImage) {
		this.coverImage = coverImage;
	}

	public String getCoverImageFileName() {
		return coverImageFileName;
	}

	public void setCoverImageFileName(String coverImageFileName) {
		this.coverImageFileName = coverImageFileName;
	}

	public String getCoverImageUrl() {
		return coverImageUrl;
	}

	public void setCoverImageUrl(String coverImageUrl) {
		this.coverImageUrl = coverImageUrl;
	}


}
